﻿using System;
using System.Threading;

namespace DamCommander
{
    class Program
    {
        static void Main(string[] args)
        {
         //   int width = 150;
           // int height = 75;
          //  Console.SetWindowSize(width, height);
            Logo logo = new Logo();
            Panel panel1 = new Panel(0, 3, 40, 20, "C:\\");
            Panel panel2 = new Panel(40, 3, 40, 20, "C:\\");
            Footer foot = new Footer();
            Popup popupF1 = new Popup(1);
            bool end = false;
            ConsoleKeyInfo myKey;
            bool popupActive = false;
            int visualX = 1; // Just for the visual effect
            int headerLines = 2; // Lines of the panel header
            //string[] prueba = new string[2];
            //prueba[0] = "f1";
            //prueba[1] = "f2";


            //------------ Drawing base screen and activate panel 1 --------------
            logo.Draw();
            panel1.Activate();
            panel1.Redraw();
            panel2.Redraw();
            foot.Draw();

            Console.SetCursorPosition(visualX, panel1.GetY()+headerLines);

            // Handle keypresses:
            // If UpArrow: Go Up in current panel - Done
            // If DownArrow: Go Down in current panel - Done
            // If Intro: Launch current file or open current directory - Done
            // If Tab: switch active panel - Done
            // If F10: exit (ask for confirmation) - Done

            //---------------------------- Keypresss ---------------------------
            do
            {
                if (Console.KeyAvailable)
                {
                    myKey = Console.ReadKey();

                    if (myKey.Key == ConsoleKey.UpArrow) // Cursor UP
                        if (panel1.GetActive())
                            panel1.MoveUp();
                        else
                            panel2.MoveUp();
                    else if (myKey.Key == ConsoleKey.DownArrow) // Cursor DOWN
                        if (panel1.GetActive())
                            panel1.MoveDown();
                        else
                            panel2.MoveDown();
                    else if (myKey.Key == ConsoleKey.Tab) // TAB key - Change Panel and redraw window
                        if (panel1.GetActive())
                        {
                            ChangePanel(ref panel1, ref panel2, foot);
                            logo.Draw();
                        }
                        else
                        {
                            ChangePanel(ref panel2, ref panel1, foot);
                            logo.Draw();
                        }
                    else if (myKey.Key == ConsoleKey.F10) // F10 key - Exit
                    {
                        end = Exit();
                            
                        if (! end)
                        {
                            Console.Clear();
                            logo.Draw();
                            panel1.Redraw();
                            panel2.Redraw();
                            foot.Draw();
                            Console.SetCursorPosition(panel1.GetX() + visualX, panel1.GetY() + headerLines);
                        }
                    }
                    else if (myKey.Key == ConsoleKey.F1) // F1 Popup
                    {
                        popupF1.ShowPopup("red", "blue");
                        popupActive = true;
                    }
                    else if (myKey.Key == ConsoleKey.Escape) // Escape - Exit popup
                    {
                        if (popupActive)
                        {
                            Console.Clear();
                            logo.Draw();
                            panel1.Redraw();
                            panel2.Redraw();
                            foot.Draw();
                            popupActive = false;
                            Console.SetCursorPosition(panel1.GetX() + visualX, panel1.GetY() + headerLines);
                        }
                    }
                    else if (myKey.Key == ConsoleKey.Enter) // Enter - Launch Item
                    {
                        if (panel1.GetActive())
                            panel1.LaunchCurrentItem();
                        else
                            panel2.LaunchCurrentItem();
                    }
                    else // Do nothing
                    {
                        if (panel1.GetActive())
                            Console.SetCursorPosition(panel1.GetCurrentX() + visualX, panel1.GetCurrentY());
                        else
                            Console.SetCursorPosition(panel2.GetCurrentX() + visualX, panel2.GetCurrentY());
                    }
                } // Enf if (Console.KeyAvailable)
            } while ( ! end ); // End of the do...while

            Bye();

        } // End of Main


        //------------------------- Functions -----------------------------------------
        
        static void Bye()
        {
            EnhancedConsole ec = new EnhancedConsole();
            Console.Clear();
            ec.Write(40 - 2, 12, "Bye!", "yellow");
            Thread.Sleep(1000);
        } // End Bye function

        static void ChangePanel(ref Panel active, ref Panel inactive, Footer foot)
        {
            Console.Clear(); 
            active.DeActivate();
            inactive.Activate();
            active.Redraw();
            inactive.Redraw();
            foot.Draw();
            Console.SetCursorPosition(inactive.GetX() + 1, inactive.GetY() + 2); // Header lines = 2 
                                                                    // The X position is just visual
        } // End ChangePanel (and redraw) function

        static bool Exit()
        {
            string exitMessage = "Are you sure that you want to exit? (Y/N) ";
            EnhancedConsole ec = new EnhancedConsole();
            Console.Clear();
            ec.Write(40 - exitMessage.Length / 2, 12, exitMessage, "yellow"); 
            ConsoleKeyInfo myKey = Console.ReadKey();
            if (myKey.Key == ConsoleKey.Y)
                return true;
            if (myKey.Key == ConsoleKey.N)
                return false;
            return false;
        } // End Exit function
    } // End of class DamCommander
} // End of NameSpace
